package persistence;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import models.beans.User;

public class UserDAO {
	private Connection con;
	private PreparedStatement pstmt;
	
	public UserDAO() {
		con = DAO.getInstance().getConnection();
	}
	
	public boolean cadastrarUsuario(User u){
		try {
			pstmt = con.prepareStatement("INSERT INTO Cliente(idConta, nome, cpf, email, isVip) VALUES (?, ?, ?, ?, ?)");
		
			pstmt.setInt(1, u.getIdConta());
			pstmt.setString(2, u.getNome());
			pstmt.setString(3, u.getCpf());
			pstmt.setString(4, u.getEmail());
			pstmt.setBoolean(5, u.isVip());
			
			return pstmt.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return false;
	}
	
	public int getId(User u){
		int idConta = 0;
		
		try {
			pstmt = con.prepareStatement("SELECT idConta FROM Cliente WHERE login = ? AND senha = ?");
		
			ResultSet rs = pstmt.executeQuery();
			while(rs.next()){
				idConta = rs.getInt(1);
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return idConta;
	}
	
}
